{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from ptrail.core.TrajectoryDF import PTRAILDataFrame\n",
    "from ptrail.preprocessing.filters import Filters as filters\n",
    "from ptrail.features.kinematic_features import KinematicFeatures\n",
    "from ptrail.features.temporal_features import TemporalFeatures\n",
    "from ptrail.preprocessing.interpolation import Interpolation\n",
    "from ptrail.utilities.conversions import Conversions as con\n",
    "import ptrail.utilities.constants as const\n",
    "import datetime as dt\n",
    "\n",
    "# Reading the gulls dataset and converting to PTRAILDataFrame.\n",
    "# Also, lets, print the first 5 points of the dataset to\n",
    "# see how the dataframe looks.\n",
    "gulls = pd.read_csv('./data/gulls.csv')\n",
    "gulls = PTRAILDataFrame(gulls,\n",
    "                        latitude='location-lat',\n",
    "                        longitude='location-long',\n",
    "                        datetime='timestamp',\n",
    "                        traj_id='tag-local-identifier',\n",
    "                        rest_of_columns=[])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [],
   "source": [
    "pdf = pd.read_csv('./data/geolife_sample.csv')\n",
    "geolife = PTRAILDataFrame(data_set=pdf,\n",
    "                             latitude='lat',\n",
    "                             longitude='lon',\n",
    "                             datetime='datetime',\n",
    "                             traj_id='id')\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [],
   "source": [
    "atlantic = pd.read_csv('./data/atlantic.csv')\n",
    "atlantic = con.convert_directions_to_degree_lat_lon(atlantic, 'Latitude',\"Longitude\")\n",
    "def convert_to_datetime(row):\n",
    "        this_date = '{}-{}-{}'.format(str(row['Date'])[0:4], str(row['Date'])[4:6], str(row['Date'])[6:])\n",
    "        this_time = '{:02d}:{:02d}:00'.format(int(row['Time']/100), int(str(row['Time'])[-2:]))\n",
    "        return '{} {}'.format(this_date, this_time)\n",
    "atlantic['DateTime'] = atlantic.apply(convert_to_datetime, axis=1)\n",
    "atlantic = PTRAILDataFrame(atlantic,\n",
    "                          latitude='Latitude',\n",
    "                          longitude='Longitude',\n",
    "                          datetime='DateTime',\n",
    "                          traj_id='ID',\n",
    "                          rest_of_columns=[])\n",
    "atlantic = TemporalFeatures.create_date_column(atlantic)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [],
   "source": [
    "pdf = pd.read_csv('./data/starkey.csv')\n",
    "starkey = PTRAILDataFrame(data_set=pdf,\n",
    "                          latitude='lat',\n",
    "                          longitude='lon',\n",
    "                          datetime='DateTime',\n",
    "                          traj_id='Id')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [],
   "source": [
    "t1 = gulls.reset_index().loc[gulls.reset_index()[const.TRAJECTORY_ID] == '91732'][[const.TRAJECTORY_ID, const.DateTime, const.LAT, const.LONG]]\n",
    "t1 = PTRAILDataFrame(data_set=t1,\n",
    "                     datetime='DateTime',\n",
    "                     traj_id='traj_id',\n",
    "                     latitude='lat',\n",
    "                     longitude='lon')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "outputs": [],
   "source": [
    "def segment_traj_by_week(df):\n",
    "    \"\"\"\n",
    "        Given a dataframe containing trajectory data, segment all\n",
    "        the trajectories by each week.\n",
    "\n",
    "        Parameters\n",
    "        ----------\n",
    "            df: PTRAILDataFrame\n",
    "                The dataframe containing trajectory data.\n",
    "\n",
    "        Returns\n",
    "        -------\n",
    "            pandas.core.dataframe.DataFrame:\n",
    "                The dataframe containing segmented trajectories\n",
    "                with a new column added called segment_id\n",
    "    \"\"\"\n",
    "    # First, create the date column and get all the unique traj_ids\n",
    "    # in the dataframe..\n",
    "    df = TemporalFeatures.create_date_column(df)\n",
    "    ids_ = list(df.traj_id.value_counts().keys())\n",
    "\n",
    "    # Get the ideal number of IDs by which the dataframe is to be split.\n",
    "    df_chunks = []\n",
    "    for i in range(len(ids_)):\n",
    "        small_df = df.reset_index().loc[df.reset_index()[const.TRAJECTORY_ID] == ids_[i]]\n",
    "        df_chunks.append(small_df)\n",
    "\n",
    "    # Now, iterate over the entire dataframe and then segment\n",
    "    # the trajectories by 1 week each.\n",
    "    results = []\n",
    "    for i in range(len(ids_)):\n",
    "        # Take the traj_df of a single Trajectory out from the\n",
    "        # list of chunks and find their max and min timestamps.\n",
    "        traj = df_chunks[i]\n",
    "        t_max = traj.reset_index()[const.DateTime].max()\n",
    "        t_min = traj.reset_index()[const.DateTime].min()\n",
    "\n",
    "        # For iteration purposes, set t_1 to min and t_2 to\n",
    "        # t_1 + 7 days.\n",
    "        t_1 = t_min\n",
    "        t_2 = t_1 + dt.timedelta(days=7)\n",
    "        seg_id = 1\n",
    "\n",
    "        # Now, segment the trajectories into smaller segments\n",
    "        # wherein each segment contains the points of a span\n",
    "        # of 7 days only.\n",
    "        while t_2 < t_max:\n",
    "            if t_2 < t_max:\n",
    "                seg = filters.filter_by_date(traj,\n",
    "                                             start_date=t_1.strftime('%Y-%m-%d'),\n",
    "                                             end_date=t_2.strftime('%Y-%m-%d'))\n",
    "                # Once filtered, assign the segment with a segment ID.\n",
    "                seg['seg_id'] = seg_id\n",
    "\n",
    "                # Increment the segment id, t_1 and t_2 values by\n",
    "                # 1, 7 days and 7 days respectively to continue\n",
    "                # the iteration.\n",
    "                t_1 += dt.timedelta(days=7)\n",
    "                t_2 += dt.timedelta(days=7)\n",
    "                results.append(seg.drop(columns=['index', 'level_0']))\n",
    "\n",
    "            # If, t_2 is greater than the max time present in the\n",
    "            # trajectory, then assign t_2 = max and proceed\n",
    "            # further with segmentation.\n",
    "            elif t_2 >= t_max:\n",
    "                seg = filters.filter_by_date(traj,\n",
    "                                             start_date=t_1.strftime('%Y-%m-%d'),\n",
    "                                             end_date=t_max.strftime('%Y-%m-%d'))\n",
    "                # Once filtered, assign the segment with a segment ID.\n",
    "                seg['seg_id'] = seg_id\n",
    "\n",
    "                # Increment the segment id, t_1 and t_2 values by\n",
    "                # 1, 7 days and 7 days respectively to continue\n",
    "                # the iteration.\n",
    "                t_1 += dt.timedelta(days=7)\n",
    "                t_2 += dt.timedelta(days=7)\n",
    "                results.append(seg.drop(columns=['index', 'level_0']))\n",
    "            seg_id += 1\n",
    "\n",
    "    # Finally, concat the dataframes, set the index as\n",
    "    # [traj_id, seg_id, DateTime].\n",
    "    return pd.concat(results).reset_index().set_index(['traj_id', 'seg_id', 'DateTime'])"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 2min 35s, sys: 652 ms, total: 2min 36s\n",
      "Wall time: 2min 36s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "seg = segment_traj_by_week(starkey)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "287136\n"
     ]
    }
   ],
   "source": [
    "print(len(starkey))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4976\n",
      "CPU times: user 41.2 s, sys: 16 ms, total: 41.2 s\n",
      "Wall time: 41.2 s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "\n",
    "# Now, generating the stats of kinematic features for\n",
    "# all the segments of the trajectories.\n",
    "\n",
    "# First, splitting the dataframe into several parts\n",
    "# wherein each part has a segment of the trajectory only.\n",
    "ids_ = list(seg.reset_index()['traj_id'].value_counts().keys())\n",
    "\n",
    "df_chunks = []\n",
    "for i in range(len(ids_)):\n",
    "    small_df = seg.reset_index().loc[seg.reset_index()[const.TRAJECTORY_ID] == ids_[i]]\n",
    "    df_chunks.append(small_df)\n",
    "\n",
    "final_chunks = []\n",
    "for i in range(len(ids_)):\n",
    "    seg_ids = list(df_chunks[i]['seg_id'].value_counts().keys())\n",
    "    for j in range(len(seg_ids)):\n",
    "        mini_df = df_chunks[i].loc[df_chunks[i]['seg_id'] == seg_ids[j]]\n",
    "        final_chunks.append(mini_df)\n",
    "\n",
    "print(len(final_chunks))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [],
   "source": [
    "def segment_features(df):\n",
    "    \"\"\"\n",
    "        Generate all the kinematic features on the smaller segments.\n",
    "\n",
    "        Parameters\n",
    "        ----------\n",
    "            df: PTRAILDataFrame/pd.DataFrame\n",
    "                The dataframe containing the trajectory segment.\n",
    "\n",
    "        Returns\n",
    "        -------\n",
    "            pd.core.dataframe.DataFrame:\n",
    "                The dataframe containing the segment kinematic\n",
    "                feature stats.\n",
    "    \"\"\"\n",
    "    seg_id = final_chunks[i]['seg_id'].iloc[0]\n",
    "    stat_df = KinematicFeatures.generate_kinematic_stats(df)\n",
    "    stat_df['seg_id'] = seg_id\n",
    "    stat_df = stat_df.reset_index().set_index(['traj_id', 'seg_id', 'Columns'])\n",
    "\n",
    "    return stat_df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "| |                                               #| 4975 Elapsed Time: 0:16:24"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 7min 26s, sys: 6min 10s, total: 13min 37s\n",
      "Wall time: 16min 25s\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "# Now, generating kinematic features and their stats on each of\n",
    "# the dataframe here.\n",
    "import time\n",
    "import progressbar\n",
    "\n",
    "final_results = []\n",
    "bar = progressbar.ProgressBar(max_value=progressbar.UnknownLength)\n",
    "for i in range(len(final_chunks)):\n",
    "    final_results.append(segment_features(final_chunks[i]))\n",
    "    bar.update(i)\n",
    "\n",
    "final_answer = pd.concat(final_results)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "data": {
      "text/plain": "                                               mean           std  \\\ntraj_id   seg_id Columns                                            \n880109D01 1      Acceleration          1.600362e-04  1.088466e-03   \n                 Bearing               1.750986e+02  1.051646e+02   \n                 Bearing_Rate          1.171464e-02  1.029251e-01   \n                 Distance              2.914998e+02  2.839370e+02   \n                 Distance_from_start   1.025254e+03  4.617076e+02   \n                 Jerk                  4.409422e-07  3.131269e-06   \n                 Rate_of_bearing_rate  1.171464e-02  1.029251e-01   \n                 Speed                 1.354668e-01  3.995508e-01   \n880119D02 1      Acceleration          2.254526e-05  2.255140e-04   \n                 Bearing               1.717234e+02  1.024833e+02   \n                 Bearing_Rate         -1.038254e-03  1.049304e-01   \n                 Distance              1.181248e+02  1.063050e+02   \n                 Distance_from_start   4.346468e+02  2.579344e+02   \n                 Jerk                  6.331631e-08  7.623935e-07   \n                 Rate_of_bearing_rate -1.038254e-03  1.049304e-01   \n                 Speed                 6.061299e-02  9.014658e-02   \n          2      Acceleration          8.416691e-05  1.114423e-03   \n                 Bearing               1.743838e+02  1.053569e+02   \n                 Bearing_Rate          4.741775e-03  1.506678e-01   \n                 Distance              1.158744e+02  1.047429e+02   \n                 Distance_from_start   4.333041e+02  2.673240e+02   \n                 Jerk                  6.566782e-07  1.044940e-05   \n                 Rate_of_bearing_rate  4.741775e-03  1.506678e-01   \n                 Speed                 6.977280e-02  1.653332e-01   \n          3      Acceleration          4.060000e-06  7.022555e-05   \n                 Bearing               1.546541e+02  1.095864e+02   \n                 Bearing_Rate         -1.487298e-03  7.853078e-02   \n                 Distance              1.794725e+02  2.294682e+02   \n                 Distance_from_start   4.895102e+02  2.474866e+02   \n                 Jerk                  4.171469e-09  7.560761e-08   \n                 Rate_of_bearing_rate -1.487298e-03  7.853078e-02   \n                 Speed                 7.135458e-02  1.071893e-01   \n          4      Acceleration          8.781777e-04  1.401964e-02   \n                 Bearing               1.627084e+02  1.104710e+02   \n                 Bearing_Rate          1.088292e-02  4.437433e-01   \n                 Distance              1.062983e+02  1.244459e+02   \n                 Distance_from_start   6.443007e+02  1.121377e+02   \n                 Jerk                  3.053720e-05  5.018339e-04   \n                 Rate_of_bearing_rate  1.088292e-02  4.437433e-01   \n                 Speed                 7.798193e-02  4.027918e-01   \n\n                                                min           10%  \\\ntraj_id   seg_id Columns                                            \n880109D01 1      Acceleration         -1.353523e-03 -2.075748e-05   \n                 Bearing               0.000000e+00  3.569276e+01   \n                 Bearing_Rate         -1.784676e-01 -8.469952e-02   \n                 Distance              0.000000e+00  4.238471e+01   \n                 Distance_from_start   5.984499e+01  2.543090e+02   \n                 Jerk                 -4.075974e-06 -1.168146e-08   \n                 Rate_of_bearing_rate -1.784676e-01 -8.469952e-02   \n                 Speed                 0.000000e+00  7.619797e-03   \n880119D02 1      Acceleration         -4.194241e-04 -2.843878e-05   \n                 Bearing               0.000000e+00  4.381483e+01   \n                 Bearing_Rate         -6.716448e-01 -9.562361e-02   \n                 Distance              0.000000e+00  2.992252e+01   \n                 Distance_from_start   2.992252e+01  8.476971e+01   \n                 Jerk                 -1.674557e-06 -2.208970e-08   \n                 Rate_of_bearing_rate -6.716448e-01 -9.562361e-02   \n                 Speed                 0.000000e+00  1.274781e-02   \n          2      Acceleration         -9.260542e-04 -2.747995e-05   \n                 Bearing               0.000000e+00  4.381497e+01   \n                 Bearing_Rate         -5.245814e-01 -1.093502e-01   \n                 Distance              0.000000e+00  2.992252e+01   \n                 Distance_from_start   2.992253e+01  1.200897e+02   \n                 Jerk                 -9.046591e-06 -2.306322e-08   \n                 Rate_of_bearing_rate -5.245814e-01 -1.093502e-01   \n                 Speed                 0.000000e+00  1.189762e-02   \n          3      Acceleration         -2.576603e-04 -2.685291e-05   \n                 Bearing               0.000000e+00  2.493856e+01   \n                 Bearing_Rate         -1.300799e-01 -1.015446e-01   \n                 Distance              0.000000e+00  2.992252e+01   \n                 Distance_from_start   3.002244e+01  1.065065e+02   \n                 Jerk                 -2.105140e-07 -2.985112e-08   \n                 Rate_of_bearing_rate -1.300799e-01 -1.015446e-01   \n                 Speed                 0.000000e+00  8.091509e-03   \n          4      Acceleration         -2.836835e-03 -2.823076e-05   \n                 Bearing               0.000000e+00  0.000000e+00   \n                 Bearing_Rate         -2.962327e+00 -9.946140e-02   \n                 Distance              0.000000e+00  0.000000e+00   \n                 Distance_from_start   4.238478e+02  5.315036e+02   \n                 Jerk                 -1.028292e-04 -2.295417e-08   \n                 Rate_of_bearing_rate -2.962327e+00 -9.946140e-02   \n                 Speed                 0.000000e+00  0.000000e+00   \n\n                                                25%           50%  \\\ntraj_id   seg_id Columns                                            \n880109D01 1      Acceleration         -7.185395e-06  1.204837e-06   \n                 Bearing               8.890776e+01  1.789141e+02   \n                 Bearing_Rate         -2.614909e-02 -2.168875e-03   \n                 Distance              9.465103e+01  1.820318e+02   \n                 Distance_from_start   7.258827e+02  1.105542e+03   \n                 Jerk                 -1.428658e-09  9.664389e-10   \n                 Rate_of_bearing_rate -2.614909e-02 -2.168875e-03   \n                 Speed                 2.192973e-02  4.670731e-02   \n880119D02 1      Acceleration         -1.190710e-05 -5.220687e-07   \n                 Bearing               7.920387e+01  1.789226e+02   \n                 Bearing_Rate         -4.245844e-02  1.429909e-07   \n                 Distance              4.239021e+01  8.976755e+01   \n                 Distance_from_start   1.986471e+02  4.494796e+02   \n                 Jerk                 -7.946190e-09  2.254356e-09   \n                 Rate_of_bearing_rate -4.245844e-02  1.429909e-07   \n                 Speed                 1.936283e-02  3.643141e-02   \n          2      Acceleration         -1.190176e-05 -8.482275e-08   \n                 Bearing               8.890696e+01  1.789208e+02   \n                 Bearing_Rate         -5.571585e-02 -6.315837e-08   \n                 Distance              4.239022e+01  9.465135e+01   \n                 Distance_from_start   1.919997e+02  3.839996e+02   \n                 Jerk                 -7.931838e-09  2.881277e-09   \n                 Rate_of_bearing_rate -5.571585e-02 -6.315837e-08   \n                 Speed                 1.815687e-02  4.141170e-02   \n          3      Acceleration         -7.978295e-06 -5.858386e-08   \n                 Bearing               6.311127e+01  1.308375e+02   \n                 Bearing_Rate         -6.241422e-02 -6.663140e-03   \n                 Distance              4.239017e+01  9.465124e+01   \n                 Distance_from_start   2.327099e+02  6.012781e+02   \n                 Jerk                 -5.795037e-09  3.040698e-10   \n                 Rate_of_bearing_rate -6.241422e-02 -6.663140e-03   \n                 Speed                 1.617005e-02  4.221507e-02   \n          4      Acceleration         -8.795917e-06  0.000000e+00   \n                 Bearing               7.041718e+01  1.524256e+02   \n                 Bearing_Rate         -6.330752e-02  0.000000e+00   \n                 Distance              3.002240e+01  6.695685e+01   \n                 Distance_from_start   5.915952e+02  6.307690e+02   \n                 Jerk                 -6.203369e-09  1.030493e-09   \n                 Rate_of_bearing_rate -6.330752e-02  0.000000e+00   \n                 Speed                 1.435131e-02  2.998211e-02   \n\n                                                75%           90%  \\\ntraj_id   seg_id Columns                                            \n880109D01 1      Acceleration          1.077288e-05  3.327739e-05   \n                 Bearing               2.770683e+02  3.050340e+02   \n                 Bearing_Rate          3.562847e-02  7.002811e-02   \n                 Distance              4.074363e+02  7.308026e+02   \n                 Distance_from_start   1.407007e+03  1.545428e+03   \n                 Jerk                  6.969048e-09  2.513570e-08   \n                 Rate_of_bearing_rate  3.562847e-02  7.002811e-02   \n                 Speed                 8.940648e-02  1.480001e-01   \n880119D02 1      Acceleration          1.273916e-05  3.473910e-05   \n                 Bearing               2.689072e+02  2.784053e+02   \n                 Bearing_Rate          5.352021e-02  9.400232e-02   \n                 Distance              1.615981e+02  2.403196e+02   \n                 Distance_from_start   6.371936e+02  7.518262e+02   \n                 Jerk                  1.057771e-08  2.680809e-08   \n                 Rate_of_bearing_rate  5.352021e-02  9.400232e-02   \n                 Speed                 7.339787e-02  1.160010e-01   \n          2      Acceleration          1.372345e-05  2.953490e-05   \n                 Bearing               2.689100e+02  3.026959e+02   \n                 Bearing_Rate          6.289347e-02  1.021810e-01   \n                 Distance              1.499416e+02  2.280120e+02   \n                 Distance_from_start   6.844812e+02  7.976403e+02   \n                 Jerk                  1.343640e-08  2.656402e-08   \n                 Rate_of_bearing_rate  6.289347e-02  1.021810e-01   \n                 Speed                 6.490975e-02  1.130406e-01   \n          3      Acceleration          9.595577e-06  3.638152e-05   \n                 Bearing               2.581735e+02  2.824424e+02   \n                 Bearing_Rate          7.393639e-02  9.219420e-02   \n                 Distance              1.986121e+02  5.461529e+02   \n                 Distance_from_start   6.737026e+02  6.933262e+02   \n                 Jerk                  1.328728e-08  3.973907e-08   \n                 Rate_of_bearing_rate  7.393639e-02  9.219420e-02   \n                 Speed                 8.746859e-02  1.528206e-01   \n          4      Acceleration          1.079411e-05  2.899869e-05   \n                 Bearing               2.689067e+02  3.049513e+02   \n                 Bearing_Rate          6.571535e-02  9.009095e-02   \n                 Distance              1.271706e+02  2.449110e+02   \n                 Distance_from_start   6.837159e+02  7.459096e+02   \n                 Jerk                  9.842042e-09  2.535588e-08   \n                 Rate_of_bearing_rate  6.571535e-02  9.009095e-02   \n                 Speed                 6.168294e-02  1.241750e-01   \n\n                                                max  \ntraj_id   seg_id Columns                             \n880109D01 1      Acceleration          6.937008e-03  \n                 Bearing               3.589162e+02  \n                 Bearing_Rate          5.478428e-01  \n                 Distance              1.224551e+03  \n                 Distance_from_start   1.810244e+03  \n                 Jerk                  2.155671e-05  \n                 Rate_of_bearing_rate  5.478428e-01  \n                 Speed                 2.767825e+00  \n880119D02 1      Acceleration          2.833763e-03  \n                 Bearing               3.589209e+02  \n                 Bearing_Rate          5.068261e-01  \n                 Distance              7.380005e+02  \n                 Distance_from_start   1.209318e+03  \n                 Jerk                  1.069896e-05  \n                 Rate_of_bearing_rate  5.068261e-01  \n                 Speed                 8.564520e-01  \n          2      Acceleration          1.761946e-02  \n                 Bearing               3.589227e+02  \n                 Bearing_Rate          1.782255e+00  \n                 Distance              7.154812e+02  \n                 Distance_from_start   1.081430e+03  \n                 Jerk                  1.677367e-04  \n                 Rate_of_bearing_rate  1.782255e+00  \n                 Speed                 1.913056e+00  \n          3      Acceleration          2.133750e-04  \n                 Bearing               3.589192e+02  \n                 Bearing_Rate          1.804863e-01  \n                 Distance              9.933759e+02  \n                 Distance_from_start   9.119628e+02  \n                 Jerk                  3.117747e-07  \n                 Rate_of_bearing_rate  1.804863e-01  \n                 Speed                 6.113082e-01  \n          4      Acceleration          2.294542e-01  \n                 Bearing               3.589192e+02  \n                 Bearing_Rate          6.494809e+00  \n                 Distance              9.933759e+02  \n                 Distance_from_start   1.272052e+03  \n                 Jerk                  8.199120e-03  \n                 Rate_of_bearing_rate  6.494809e+00  \n                 Speed                 6.501132e+00  ",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th></th>\n      <th></th>\n      <th>mean</th>\n      <th>std</th>\n      <th>min</th>\n      <th>10%</th>\n      <th>25%</th>\n      <th>50%</th>\n      <th>75%</th>\n      <th>90%</th>\n      <th>max</th>\n    </tr>\n    <tr>\n      <th>traj_id</th>\n      <th>seg_id</th>\n      <th>Columns</th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n      <th></th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th rowspan=\"8\" valign=\"top\">880109D01</th>\n      <th rowspan=\"8\" valign=\"top\">1</th>\n      <th>Acceleration</th>\n      <td>1.600362e-04</td>\n      <td>1.088466e-03</td>\n      <td>-1.353523e-03</td>\n      <td>-2.075748e-05</td>\n      <td>-7.185395e-06</td>\n      <td>1.204837e-06</td>\n      <td>1.077288e-05</td>\n      <td>3.327739e-05</td>\n      <td>6.937008e-03</td>\n    </tr>\n    <tr>\n      <th>Bearing</th>\n      <td>1.750986e+02</td>\n      <td>1.051646e+02</td>\n      <td>0.000000e+00</td>\n      <td>3.569276e+01</td>\n      <td>8.890776e+01</td>\n      <td>1.789141e+02</td>\n      <td>2.770683e+02</td>\n      <td>3.050340e+02</td>\n      <td>3.589162e+02</td>\n    </tr>\n    <tr>\n      <th>Bearing_Rate</th>\n      <td>1.171464e-02</td>\n      <td>1.029251e-01</td>\n      <td>-1.784676e-01</td>\n      <td>-8.469952e-02</td>\n      <td>-2.614909e-02</td>\n      <td>-2.168875e-03</td>\n      <td>3.562847e-02</td>\n      <td>7.002811e-02</td>\n      <td>5.478428e-01</td>\n    </tr>\n    <tr>\n      <th>Distance</th>\n      <td>2.914998e+02</td>\n      <td>2.839370e+02</td>\n      <td>0.000000e+00</td>\n      <td>4.238471e+01</td>\n      <td>9.465103e+01</td>\n      <td>1.820318e+02</td>\n      <td>4.074363e+02</td>\n      <td>7.308026e+02</td>\n      <td>1.224551e+03</td>\n    </tr>\n    <tr>\n      <th>Distance_from_start</th>\n      <td>1.025254e+03</td>\n      <td>4.617076e+02</td>\n      <td>5.984499e+01</td>\n      <td>2.543090e+02</td>\n      <td>7.258827e+02</td>\n      <td>1.105542e+03</td>\n      <td>1.407007e+03</td>\n      <td>1.545428e+03</td>\n      <td>1.810244e+03</td>\n    </tr>\n    <tr>\n      <th>Jerk</th>\n      <td>4.409422e-07</td>\n      <td>3.131269e-06</td>\n      <td>-4.075974e-06</td>\n      <td>-1.168146e-08</td>\n      <td>-1.428658e-09</td>\n      <td>9.664389e-10</td>\n      <td>6.969048e-09</td>\n      <td>2.513570e-08</td>\n      <td>2.155671e-05</td>\n    </tr>\n    <tr>\n      <th>Rate_of_bearing_rate</th>\n      <td>1.171464e-02</td>\n      <td>1.029251e-01</td>\n      <td>-1.784676e-01</td>\n      <td>-8.469952e-02</td>\n      <td>-2.614909e-02</td>\n      <td>-2.168875e-03</td>\n      <td>3.562847e-02</td>\n      <td>7.002811e-02</td>\n      <td>5.478428e-01</td>\n    </tr>\n    <tr>\n      <th>Speed</th>\n      <td>1.354668e-01</td>\n      <td>3.995508e-01</td>\n      <td>0.000000e+00</td>\n      <td>7.619797e-03</td>\n      <td>2.192973e-02</td>\n      <td>4.670731e-02</td>\n      <td>8.940648e-02</td>\n      <td>1.480001e-01</td>\n      <td>2.767825e+00</td>\n    </tr>\n    <tr>\n      <th rowspan=\"32\" valign=\"top\">880119D02</th>\n      <th rowspan=\"8\" valign=\"top\">1</th>\n      <th>Acceleration</th>\n      <td>2.254526e-05</td>\n      <td>2.255140e-04</td>\n      <td>-4.194241e-04</td>\n      <td>-2.843878e-05</td>\n      <td>-1.190710e-05</td>\n      <td>-5.220687e-07</td>\n      <td>1.273916e-05</td>\n      <td>3.473910e-05</td>\n      <td>2.833763e-03</td>\n    </tr>\n    <tr>\n      <th>Bearing</th>\n      <td>1.717234e+02</td>\n      <td>1.024833e+02</td>\n      <td>0.000000e+00</td>\n      <td>4.381483e+01</td>\n      <td>7.920387e+01</td>\n      <td>1.789226e+02</td>\n      <td>2.689072e+02</td>\n      <td>2.784053e+02</td>\n      <td>3.589209e+02</td>\n    </tr>\n    <tr>\n      <th>Bearing_Rate</th>\n      <td>-1.038254e-03</td>\n      <td>1.049304e-01</td>\n      <td>-6.716448e-01</td>\n      <td>-9.562361e-02</td>\n      <td>-4.245844e-02</td>\n      <td>1.429909e-07</td>\n      <td>5.352021e-02</td>\n      <td>9.400232e-02</td>\n      <td>5.068261e-01</td>\n    </tr>\n    <tr>\n      <th>Distance</th>\n      <td>1.181248e+02</td>\n      <td>1.063050e+02</td>\n      <td>0.000000e+00</td>\n      <td>2.992252e+01</td>\n      <td>4.239021e+01</td>\n      <td>8.976755e+01</td>\n      <td>1.615981e+02</td>\n      <td>2.403196e+02</td>\n      <td>7.380005e+02</td>\n    </tr>\n    <tr>\n      <th>Distance_from_start</th>\n      <td>4.346468e+02</td>\n      <td>2.579344e+02</td>\n      <td>2.992252e+01</td>\n      <td>8.476971e+01</td>\n      <td>1.986471e+02</td>\n      <td>4.494796e+02</td>\n      <td>6.371936e+02</td>\n      <td>7.518262e+02</td>\n      <td>1.209318e+03</td>\n    </tr>\n    <tr>\n      <th>Jerk</th>\n      <td>6.331631e-08</td>\n      <td>7.623935e-07</td>\n      <td>-1.674557e-06</td>\n      <td>-2.208970e-08</td>\n      <td>-7.946190e-09</td>\n      <td>2.254356e-09</td>\n      <td>1.057771e-08</td>\n      <td>2.680809e-08</td>\n      <td>1.069896e-05</td>\n    </tr>\n    <tr>\n      <th>Rate_of_bearing_rate</th>\n      <td>-1.038254e-03</td>\n      <td>1.049304e-01</td>\n      <td>-6.716448e-01</td>\n      <td>-9.562361e-02</td>\n      <td>-4.245844e-02</td>\n      <td>1.429909e-07</td>\n      <td>5.352021e-02</td>\n      <td>9.400232e-02</td>\n      <td>5.068261e-01</td>\n    </tr>\n    <tr>\n      <th>Speed</th>\n      <td>6.061299e-02</td>\n      <td>9.014658e-02</td>\n      <td>0.000000e+00</td>\n      <td>1.274781e-02</td>\n      <td>1.936283e-02</td>\n      <td>3.643141e-02</td>\n      <td>7.339787e-02</td>\n      <td>1.160010e-01</td>\n      <td>8.564520e-01</td>\n    </tr>\n    <tr>\n      <th rowspan=\"8\" valign=\"top\">2</th>\n      <th>Acceleration</th>\n      <td>8.416691e-05</td>\n      <td>1.114423e-03</td>\n      <td>-9.260542e-04</td>\n      <td>-2.747995e-05</td>\n      <td>-1.190176e-05</td>\n      <td>-8.482275e-08</td>\n      <td>1.372345e-05</td>\n      <td>2.953490e-05</td>\n      <td>1.761946e-02</td>\n    </tr>\n    <tr>\n      <th>Bearing</th>\n      <td>1.743838e+02</td>\n      <td>1.053569e+02</td>\n      <td>0.000000e+00</td>\n      <td>4.381497e+01</td>\n      <td>8.890696e+01</td>\n      <td>1.789208e+02</td>\n      <td>2.689100e+02</td>\n      <td>3.026959e+02</td>\n      <td>3.589227e+02</td>\n    </tr>\n    <tr>\n      <th>Bearing_Rate</th>\n      <td>4.741775e-03</td>\n      <td>1.506678e-01</td>\n      <td>-5.245814e-01</td>\n      <td>-1.093502e-01</td>\n      <td>-5.571585e-02</td>\n      <td>-6.315837e-08</td>\n      <td>6.289347e-02</td>\n      <td>1.021810e-01</td>\n      <td>1.782255e+00</td>\n    </tr>\n    <tr>\n      <th>Distance</th>\n      <td>1.158744e+02</td>\n      <td>1.047429e+02</td>\n      <td>0.000000e+00</td>\n      <td>2.992252e+01</td>\n      <td>4.239022e+01</td>\n      <td>9.465135e+01</td>\n      <td>1.499416e+02</td>\n      <td>2.280120e+02</td>\n      <td>7.154812e+02</td>\n    </tr>\n    <tr>\n      <th>Distance_from_start</th>\n      <td>4.333041e+02</td>\n      <td>2.673240e+02</td>\n      <td>2.992253e+01</td>\n      <td>1.200897e+02</td>\n      <td>1.919997e+02</td>\n      <td>3.839996e+02</td>\n      <td>6.844812e+02</td>\n      <td>7.976403e+02</td>\n      <td>1.081430e+03</td>\n    </tr>\n    <tr>\n      <th>Jerk</th>\n      <td>6.566782e-07</td>\n      <td>1.044940e-05</td>\n      <td>-9.046591e-06</td>\n      <td>-2.306322e-08</td>\n      <td>-7.931838e-09</td>\n      <td>2.881277e-09</td>\n      <td>1.343640e-08</td>\n      <td>2.656402e-08</td>\n      <td>1.677367e-04</td>\n    </tr>\n    <tr>\n      <th>Rate_of_bearing_rate</th>\n      <td>4.741775e-03</td>\n      <td>1.506678e-01</td>\n      <td>-5.245814e-01</td>\n      <td>-1.093502e-01</td>\n      <td>-5.571585e-02</td>\n      <td>-6.315837e-08</td>\n      <td>6.289347e-02</td>\n      <td>1.021810e-01</td>\n      <td>1.782255e+00</td>\n    </tr>\n    <tr>\n      <th>Speed</th>\n      <td>6.977280e-02</td>\n      <td>1.653332e-01</td>\n      <td>0.000000e+00</td>\n      <td>1.189762e-02</td>\n      <td>1.815687e-02</td>\n      <td>4.141170e-02</td>\n      <td>6.490975e-02</td>\n      <td>1.130406e-01</td>\n      <td>1.913056e+00</td>\n    </tr>\n    <tr>\n      <th rowspan=\"8\" valign=\"top\">3</th>\n      <th>Acceleration</th>\n      <td>4.060000e-06</td>\n      <td>7.022555e-05</td>\n      <td>-2.576603e-04</td>\n      <td>-2.685291e-05</td>\n      <td>-7.978295e-06</td>\n      <td>-5.858386e-08</td>\n      <td>9.595577e-06</td>\n      <td>3.638152e-05</td>\n      <td>2.133750e-04</td>\n    </tr>\n    <tr>\n      <th>Bearing</th>\n      <td>1.546541e+02</td>\n      <td>1.095864e+02</td>\n      <td>0.000000e+00</td>\n      <td>2.493856e+01</td>\n      <td>6.311127e+01</td>\n      <td>1.308375e+02</td>\n      <td>2.581735e+02</td>\n      <td>2.824424e+02</td>\n      <td>3.589192e+02</td>\n    </tr>\n    <tr>\n      <th>Bearing_Rate</th>\n      <td>-1.487298e-03</td>\n      <td>7.853078e-02</td>\n      <td>-1.300799e-01</td>\n      <td>-1.015446e-01</td>\n      <td>-6.241422e-02</td>\n      <td>-6.663140e-03</td>\n      <td>7.393639e-02</td>\n      <td>9.219420e-02</td>\n      <td>1.804863e-01</td>\n    </tr>\n    <tr>\n      <th>Distance</th>\n      <td>1.794725e+02</td>\n      <td>2.294682e+02</td>\n      <td>0.000000e+00</td>\n      <td>2.992252e+01</td>\n      <td>4.239017e+01</td>\n      <td>9.465124e+01</td>\n      <td>1.986121e+02</td>\n      <td>5.461529e+02</td>\n      <td>9.933759e+02</td>\n    </tr>\n    <tr>\n      <th>Distance_from_start</th>\n      <td>4.895102e+02</td>\n      <td>2.474866e+02</td>\n      <td>3.002244e+01</td>\n      <td>1.065065e+02</td>\n      <td>2.327099e+02</td>\n      <td>6.012781e+02</td>\n      <td>6.737026e+02</td>\n      <td>6.933262e+02</td>\n      <td>9.119628e+02</td>\n    </tr>\n    <tr>\n      <th>Jerk</th>\n      <td>4.171469e-09</td>\n      <td>7.560761e-08</td>\n      <td>-2.105140e-07</td>\n      <td>-2.985112e-08</td>\n      <td>-5.795037e-09</td>\n      <td>3.040698e-10</td>\n      <td>1.328728e-08</td>\n      <td>3.973907e-08</td>\n      <td>3.117747e-07</td>\n    </tr>\n    <tr>\n      <th>Rate_of_bearing_rate</th>\n      <td>-1.487298e-03</td>\n      <td>7.853078e-02</td>\n      <td>-1.300799e-01</td>\n      <td>-1.015446e-01</td>\n      <td>-6.241422e-02</td>\n      <td>-6.663140e-03</td>\n      <td>7.393639e-02</td>\n      <td>9.219420e-02</td>\n      <td>1.804863e-01</td>\n    </tr>\n    <tr>\n      <th>Speed</th>\n      <td>7.135458e-02</td>\n      <td>1.071893e-01</td>\n      <td>0.000000e+00</td>\n      <td>8.091509e-03</td>\n      <td>1.617005e-02</td>\n      <td>4.221507e-02</td>\n      <td>8.746859e-02</td>\n      <td>1.528206e-01</td>\n      <td>6.113082e-01</td>\n    </tr>\n    <tr>\n      <th rowspan=\"8\" valign=\"top\">4</th>\n      <th>Acceleration</th>\n      <td>8.781777e-04</td>\n      <td>1.401964e-02</td>\n      <td>-2.836835e-03</td>\n      <td>-2.823076e-05</td>\n      <td>-8.795917e-06</td>\n      <td>0.000000e+00</td>\n      <td>1.079411e-05</td>\n      <td>2.899869e-05</td>\n      <td>2.294542e-01</td>\n    </tr>\n    <tr>\n      <th>Bearing</th>\n      <td>1.627084e+02</td>\n      <td>1.104710e+02</td>\n      <td>0.000000e+00</td>\n      <td>0.000000e+00</td>\n      <td>7.041718e+01</td>\n      <td>1.524256e+02</td>\n      <td>2.689067e+02</td>\n      <td>3.049513e+02</td>\n      <td>3.589192e+02</td>\n    </tr>\n    <tr>\n      <th>Bearing_Rate</th>\n      <td>1.088292e-02</td>\n      <td>4.437433e-01</td>\n      <td>-2.962327e+00</td>\n      <td>-9.946140e-02</td>\n      <td>-6.330752e-02</td>\n      <td>0.000000e+00</td>\n      <td>6.571535e-02</td>\n      <td>9.009095e-02</td>\n      <td>6.494809e+00</td>\n    </tr>\n    <tr>\n      <th>Distance</th>\n      <td>1.062983e+02</td>\n      <td>1.244459e+02</td>\n      <td>0.000000e+00</td>\n      <td>0.000000e+00</td>\n      <td>3.002240e+01</td>\n      <td>6.695685e+01</td>\n      <td>1.271706e+02</td>\n      <td>2.449110e+02</td>\n      <td>9.933759e+02</td>\n    </tr>\n    <tr>\n      <th>Distance_from_start</th>\n      <td>6.443007e+02</td>\n      <td>1.121377e+02</td>\n      <td>4.238478e+02</td>\n      <td>5.315036e+02</td>\n      <td>5.915952e+02</td>\n      <td>6.307690e+02</td>\n      <td>6.837159e+02</td>\n      <td>7.459096e+02</td>\n      <td>1.272052e+03</td>\n    </tr>\n    <tr>\n      <th>Jerk</th>\n      <td>3.053720e-05</td>\n      <td>5.018339e-04</td>\n      <td>-1.028292e-04</td>\n      <td>-2.295417e-08</td>\n      <td>-6.203369e-09</td>\n      <td>1.030493e-09</td>\n      <td>9.842042e-09</td>\n      <td>2.535588e-08</td>\n      <td>8.199120e-03</td>\n    </tr>\n    <tr>\n      <th>Rate_of_bearing_rate</th>\n      <td>1.088292e-02</td>\n      <td>4.437433e-01</td>\n      <td>-2.962327e+00</td>\n      <td>-9.946140e-02</td>\n      <td>-6.330752e-02</td>\n      <td>0.000000e+00</td>\n      <td>6.571535e-02</td>\n      <td>9.009095e-02</td>\n      <td>6.494809e+00</td>\n    </tr>\n    <tr>\n      <th>Speed</th>\n      <td>7.798193e-02</td>\n      <td>4.027918e-01</td>\n      <td>0.000000e+00</td>\n      <td>0.000000e+00</td>\n      <td>1.435131e-02</td>\n      <td>2.998211e-02</td>\n      <td>6.168294e-02</td>\n      <td>1.241750e-01</td>\n      <td>6.501132e+00</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "final_answer.head(40)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}